<?php

$GLOBALS['THRIFT_ROOT'] = LIB_PATH . "/thrift";
require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );

class HbaseQueryer {
	
	private $socket;
	private $transport;
	private $client;
	private $protocol;
	
	public function connect($hbaseHost, $hbasePort) {
		try{
			//$socket = new TSocket( 'w-hdp2.safe.zzbc.qihoo.net', 9090);
			$this->socket = new TSocket($hbaseHost, $hbasePort);
			$this->socket->setSendTimeout( 1000 ); // One seconds
			$this->socket->setRecvTimeout( 20000 ); // Twenty seconds
			$this->transport = new TBufferedTransport( $this->socket );
			$this->protocol = new TBinaryProtocol( $this->transport );
			
			$this->client = new HbaseClient( $this->protocol );			
			$this->transport->open();
            return true;
		}
		catch(Exception $e){
            Yii::log("connect to hbase error: ". $e->getMessage(), CLogger::LEVEL_ERROR, EXECUTOR_LOG);
            return false;
		}
	}
	
	
	public function query($fileMd5) {
		$md5 = strtolower(trim($fileMd5));
		
		// Run a scanner on the rows we just created
		try {
			$scanner = $this->client->scannerOpenWithStop( "md5", $md5,$md5, array( "analyze:","basic:","query:" ) );
			$result= $this->client->scannerGet( $scanner );
            if(empty($result)) {
                Yii::log("query $fileMd5 failed, not found", CLogger::LEVER_INFO, EXECUTOR_LOG);
                return false;
            }
			return $this->parseResult($result);
		} 
        catch ( Exception $e ) {
            Yii::log("query $fileMd5 failed: " . $e->getMessage(), CLogger::LEVEL_ERROR, EXECUTOR_LOG);
            return false;
		}
	}
	
	public function close() {
		$this->transport->close();
	}

    private function parseResult($result) {
        
        $data=$uploads=$basic=$uploads_glob=$querys=$levels=array();
        $basic_keys=array(
            "sha1"=>"sha1",
            "upload_filesize"=>"文件大小",
            "upload_sign"=>"签名",
            "upload_sign_corp"=>"签名公司",
            "sign360"=>"360签名",
        );
        foreach($result[0]->columns as $key=>$value){
            list($column,$key)=explode(":",$key,2);
            $info=explode("_",$key);
            
            if($column=="query"){
                list(,$level)=explode("_",$key);
                $count=$value->value;
                $timestamp=$value->timestamp;
                
                $timestamp = date('Y-m-d', $timestamp);
                if(!in_array($level, $levels)) {
                    $levels[] = $level;
                }
                if(isset($querys[$timestamp])){
                    $querys[$timestamp]['count']+=$count;
                    if(!in_array($level,$querys[$timestamp]['levels'])){
                        $querys[$timestamp]['levels'][]=$level;
                    }
                }
                else{
                    $querys[$timestamp]['count']=$count;
                    $querys[$timestamp]['levels']=array($level);
                }
            }
        }
        
        return $querys;
    }

}
